﻿package ui 
{
	import Base.Loader.SWFLoader;
	import Base.Net.*;
	
	import Common.*;
	
	import Events.*;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import logic.*;
	
	import mx.controls.TextArea;
	
	
	
	/**
	 * ...
	 * @author fox
	 */
	public class ChatUIHandler extends uiBase
	{
		private var mainPlayer:GamePlayer = null;
		
		private var updateTime:Number = 10000.0;
		private var updateDelay:Number = 0.0;
		
		public function ChatUIHandler()
		{
			path = "chat.swf";
			name = "ChatUI";
		}
		
		
		public override function onInit( loader:SWFLoader ):void
		{
			super.onInit( loader );
			
			mainPlayer = GameManager.instance.MainPlayer;
			
			uiMc.chatText.selectable = false;
			uiMc.sendText.maxChars = 64;
			
			uiMc.chatText.addEventListener( MouseEvent.MOUSE_MOVE , onMouseMove );
			uiMc.chatText.addEventListener( MouseEvent.CLICK , GameScene.instance.onMouseClick );
			
			GameUIManager.instance.bottomSprite.addChild( uiMc as DisplayObject );
		}
		
		
		public override function resize():void
		{
			if ( uiMc )
			{
				uiMc.x = 5;
				uiMc.y = RenderManager.instance.stageHeight;
			}
		}
		
		
		public static var instance:ChatUIHandler = null;
		public static function Instance():ChatUIHandler
		{
			if ( instance == null )
			{
				instance = new ChatUIHandler();
			}
			
			return instance;
		}
		
		public function enableMouse( b:Boolean ):void
		{
			if ( uiMc )
			{
				uiMc.chatText.mouseEnabled = b;
			}
		}
		
		public override function update( delay:Number ):void
		{
			super.update( delay );
			
			if ( !uiMc )
			{
				return;
			}
			
			if ( uiMc.chatText.alpha <= 0.0 )
			{
				updateDelay = 0.0;
				uiMc.chatText.alpha = 0.0;
				return;
			}
			
			if ( updateDelay > updateTime )
			{
				uiMc.chatText.alpha -= 0.1;
			}
			
			updateDelay += delay;
		}
		
		public function onMouseMove( event:MouseEvent ):void
		{
			uiMc.chatText.alpha = 1.0;
			updateDelay = 0.0;
		}
		
		public function onKeyDown( event:ModuleEvent ):void
		{
			if ( uiMc )
			{
				var sss:TextField = RenderManager.instance.stage.focus as TextField;
				
				if ( sss == uiMc.sendText && event.object.keyCode == 13 )
				{
					sendChat();
				}
				
				if ( sss != uiMc.sendText && event.object.keyCode == 13 ) 
				{
					RenderManager.instance.stage.stageFocusRect = false;
					RenderManager.instance.stage.focus = uiMc.sendText;
				}
			}
			
		}
		
		public function onSendChat( event:MouseEvent ):void
		{
			sendChat();
			
			//chatSendButton.setEnabled( false );
		}
		
		public function onClearChat( event:MouseEvent ):void
		{
			uiMc.chatText.text = "";
			
			//chatSendButton.setEnabled( false );
		}
		
		
		
		public function sendChat():void
		{
			if ( uiMc.sendText.text.length == 0 ) 
			{
				return;
			}
			
			var netSocket:NetSocket = GameNetManager.instance.MainNetSocket;
			netSocket.writeShort( GameNetEvent.SEND_CHAT_SIZE );
			netSocket.writeShort( GameNetEvent.SEND_CHAT_ID );
			
			netSocket.writeInt( 0 );
			
			var text:ByteArray = new ByteArray();
			text.writeUTFBytes( uiMc.sendText.text );
			text.length = GameDefine.MAX_CHAT;
			
			var name:ByteArray = new ByteArray();
			name.writeUTFBytes( mainPlayer.Name );
			name.length = GameDefine.MAX_NAME;
			
			netSocket.writeBytes( name );
			netSocket.writeBytes( text );
			netSocket.Send();
			
			uiMc.sendText.text = "";
		}
		
		public function addText( str:String , system:Boolean = true ):void
		{
			if ( uiMc )
			{
				uiMc.chatText.wordWrap = false;
				uiMc.chatText.htmlText += "<font color = '" + GameDefine.SYSTEM_COLOR + "' >"+ str + "</font>";
				uiMc.chatText.wordWrap = true;
				
				uiMc.chatText.alpha = 1.0;
				updateDelay = 0.0;
				uiMc.chatText.scrollV = uiMc.chatText.maxScrollV;
			}
			
		}
		
		public function onRecvChat( data:ByteArray ):void
		{
			var text1:String = null;
			var name1:String = null;
			
			var type:int = data.readInt();
			
			name1 = data.readUTFBytes( GameDefine.MAX_NAME );
			text1 = data.readUTFBytes( GameDefine.MAX_CHAT );
			
			uiMc.chatText.wordWrap = false;
			uiMc.chatText.htmlText += "<font color ='#ffffff' >" + name1.toString() + ":" + text1.toString() + "</font>";
			
			if ( uiMc.chatText.numLines >= GameDefine.MAX_CHAT_LINE ) 
			{
				var offset:int = uiMc.chatText.getLineOffset( 1 );
				uiMc.chatText.replaceText( 0 , offset , "" );
			}
			
			uiMc.chatText.wordWrap = true;
			
			uiMc.chatText.alpha = 1.0;
			updateDelay = 0.0;
			uiMc.chatText.scrollV = uiMc.chatText.maxScrollV;
		}
	}
	
}
